package com.subang.function.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbWish;
import com.subang.function.service.ISbWishService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 用户愿望Controller
 * 
 * @author subei
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/function/wish")
public class SbWishController extends BaseController
{
    @Autowired
    private ISbWishService sbWishService;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询用户愿望列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SbWish sbWish)
    {
        startPage();
        List<SbWish> list = sbWishService.selectSbWishList(sbWish);
        return getDataTable(list);
    }

    /**
     * 导出用户愿望列表
     */
    @PreAuthorize("@ss.hasPermi('function:wish:export')")
    @Log(title = "用户愿望", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbWish sbWish)
    {
        List<SbWish> list = sbWishService.selectSbWishList(sbWish);
        ExcelUtil<SbWish> util = new ExcelUtil<SbWish>(SbWish.class);
        util.exportExcel(response, list, "用户愿望数据");
    }

    /**
     * 获取用户愿望详细信息
     */

    @GetMapping(value = "/{wishId}")
    public AjaxResult getInfo(@PathVariable("wishId") Long wishId)
    {
        return success(sbWishService.selectSbWishByWishId(wishId));
    }

    /**
     * 新增用户愿望
     */

    @Log(title = "用户愿望", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbWish sbWish)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sbWish.setUserId(user.getUserId());
        sbWish.setUserName(user.getNickName());
        sbWish.setUserAvatar(user.getAvatar());
        return toAjax(sbWishService.insertSbWish(sbWish));
    }

    /**
     * 修改用户愿望
     */

    @Log(title = "用户愿望", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbWish sbWish)
    {
        return toAjax(sbWishService.updateSbWish(sbWish));
    }

    /**
     * 删除用户愿望
     */

    @Log(title = "用户愿望", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wishIds}")
    public AjaxResult remove(@PathVariable Long[] wishIds)
    {
        return toAjax(sbWishService.deleteSbWishByWishIds(wishIds));
    }

    @RequestMapping("/wishCheck")
    public AjaxResult wishCheck(SbWish sbWish) {
        SysUser user = userService.getUserByOpenId(SecurityUtils.getLoginUser().getUser().getOpenid());
        sbWish.setUserId(user.getUserId());
        return success(sbWishService.selectSbWishList(sbWish));

    }
}
